package com.xca.znb.view;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.xca.znb.R;
import com.xca.znb.contact.SettingContact;

public class Setting extends AppCompatActivity implements View.OnClickListener , SettingContact {
    private ImageButton setBackBtn ;
    private LinearLayout setPersonMessage , setLocalManage , setLoginPaw , setPayPaw ;
    private LinearLayout setCallCustomer , setFeedback , setAboutUs , setAboutSoftware ;
    private Button setSignOut ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        setBackBtn = findViewById(R.id.set_back_btn);
        setPersonMessage = findViewById(R.id.setting_person_message) ;
        setLocalManage = findViewById(R.id.setting_local_manager) ;
        setLoginPaw = findViewById(R.id.setting_login_password) ;
        setPayPaw = findViewById(R.id.setting_pay_password) ;
        setCallCustomer = findViewById(R.id.setting_call_customer) ;
        setFeedback = findViewById(R.id.setting_feedback) ;
        setAboutUs = findViewById(R.id.setting_about_us) ;
        setAboutSoftware = findViewById(R.id.setting_about_software);
        setSignOut = findViewById(R.id.setting_sign_out);

        onSetBackBtnClick();
        onSetPersonMsgBtnClick();
        onSetLocalMsgBtnClick();
        onSetLoginPasswordBtnClick();
        onSetPayPasswordBtnClick();
        onSetCallCustomerBtnClick();
        onSetFeedbackBtnClick();
        onSetAboutUsBtnClick();
        onSetAboutSoftwareBtnClick();
        onSetSignOutBtnClick();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.set_back_btn){
            onBackPressed();
        } else if (v.getId() == R.id.setting_person_message) {
            Intent intent = new Intent(this , PersonalCenter.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_local_manager) {
            Intent intent = new Intent(this , LocalManagerActivity.class) ;
            startActivity(intent);
        }else if (v.getId() == R.id.setting_login_password){
            Intent intent = new Intent(this , UpdateLoginPassword.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_pay_password) {
            Intent intent = new Intent(this , UpdatePayPassword.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_call_customer) {
            showCallConfirmationDialog("18137485509"); // 替换为实际电话号码
        } else if (v.getId() == R.id.setting_feedback) {
            Intent intent = new Intent(this , FeedbackActivity.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_about_us) {
            Intent intent = new Intent(this , AboutUs.class);
            startActivity(intent);
        } else if (v.getId() == R.id.setting_about_software) {
            Intent intent = new Intent(this , AboutSoftware.class) ;
            startActivity(intent);
        } else if (v.getId() == R.id.setting_sign_out) {
            new AlertDialog.Builder(Setting.this)
                    .setMessage("是否退出登录")
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            loginOut();
                        }
                    })
                    .create().show();
        }
    }


    private void loginOut(){
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");//收藏user
        if (sharedPreferences.contains("phone")){
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove("phone");
            boolean isCleared = editor.commit();
            if (isCleared){
                Intent intent = new Intent(Setting.this , LoginActivity.class);
                startActivity(intent);
                finish();
            }else {
                Toast.makeText(this, "退出登录失败", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    public void onSetBackBtnClick() {
        setBackBtn.setOnClickListener(this);
    }

    @Override
    public void onSetPersonMsgBtnClick() {
        setPersonMessage.setOnClickListener(this);
    }

    @Override
    public void onSetLocalMsgBtnClick() {
        setLocalManage.setOnClickListener(this);
    }

    @Override
    public void onSetLoginPasswordBtnClick() {
        setLoginPaw.setOnClickListener(this);
    }

    @Override
    public void onSetPayPasswordBtnClick() {
        setPayPaw.setOnClickListener(this);
    }

    @Override
    public void onSetCallCustomerBtnClick() {
        setCallCustomer.setOnClickListener(this);
    }

    @Override
    public void onSetFeedbackBtnClick() {
        setFeedback.setOnClickListener(this);
    }

    @Override
    public void onSetAboutUsBtnClick() {
        setAboutUs.setOnClickListener(this);
    }

    @Override
    public void onSetAboutSoftwareBtnClick() {
        setAboutSoftware.setOnClickListener(this);
    }

    @Override
    public void onSetSignOutBtnClick() {
        setSignOut.setOnClickListener(this);
    }
    // 弹出确认拨打电话的对话框
    private void showCallConfirmationDialog(String phoneNumber) {
        // 检查拨打电话的权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.CALL_PHONE},
                    1);
        } else {
            confirmCall(phoneNumber);
        }
    }

    // 请求权限的结果回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 用户同意了权限请求，再次尝试拨打电话
                confirmCall("18137485509"); // 替换为实际电话号码
            } else {
                // 用户拒绝了权限请求，可以处理这个情况，比如显示一个信息
            }
        }
    }

    // 确认拨打电话
    private void confirmCall(String phoneNumber) {
        new AlertDialog.Builder(this)
                .setTitle("拨打电话")
                .setMessage("您确定要拨打这个电话吗？")
                .setPositiveButton("拨打", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:" + phoneNumber));
                        startActivity(callIntent);
                    }
                })
                .setNegativeButton("取消", null).show();
    }
}

